//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

print(str);

//let a= 1+2;//Err
//let a = 1 +2;//Err
let a=1+2;
let b = 1 + 2;
let c = 1+2;

let str1 = "abcd\(a)\(b+c)"
let str2 = "abcd\(a)\(b+c)"+String(c)
var strSplitResultArr: Array = str.components(separatedBy: ",")
let str4 = strSplitResultArr.joined(separator: "   ");

//======2016年12月05日09:50:51

var someInts = [Int](repeating: 0, count: 3)
someInts = [1, 2, 4, 5, 8];
var someInts2 = [6, 9, 10];
var someIntsCon = someInts + someInts2;
//someIntsCon = someInts2.append(20);
someInts.insert(33, at: 0);
someInts2.isEmpty;

someInts2.first;
someInts2.last;
someInts2[1];
//someInts2[41];
someInts2.append(13);
someInts2.append(contentsOf: [124, 12]);

someInts2.remove(at: 2);
someInts2.removeLast();
someInts2.removeFirst();
someInts2[0] = 111;

let colors = ["periwinkle", "rose", "moss"];
let url: NSURL = NSURL(fileURLWithPath: "names.plist")
(colors as NSArray).write(to: url as URL, atomically: true)
//colors.sorted(by: <)
colors.sorted()

let numbers = [45, 3, 23, 89, 12, 546, 33, 90, 123];
var sortedNumbers = numbers.sorted { $0 > $1 }
// 使用闭包排序
sortedNumbers = numbers.sorted(by: { (int1: Int, int2: Int) -> Bool in
    return int1<int2;
})
print(sortedNumbers)

var someDict = [1: 3, 5: 7];
someDict.updateValue(2, forKey: 1);
someDict.updateValue(2, forKey: 9);
someDict[20];
for (key, value) in someDict {
    print("\(key)->\(value)")
}

someDict.count;

// ============== Function

func sayHello(_ toUser: String)->String {
    return "Hello " + toUser;
}

func runoob(site: String) -> String {
    return site
}

sayHello("Ani")


func minMax1(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array.first;
    var currentMax = currentMin;
    
    for value in array[1..<array.count] {
        if value < currentMin! {
            currentMin = value
        } else if value > currentMax! {
            currentMax = value
        }
    }
    
    return (currentMin!, currentMax!);
}

func minMax(array: [Int]) -> (min: Int, max: Int)? {
    if array.isEmpty {
        return nil;
    }
    var currentMin = array[0];
    var currentMax = array[0];
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

let bounds = minMax1(array: numbers)
bounds.max;
bounds.min;

// 外部参数和内部参数
func minInt(valueA a: Int, valueB b:Int)->Int{
    if a>b {
        return a;
    }
    return b;
}

minInt(valueA: 89, valueB: 77)

// 可变参数
func vari<VarType>(vars:VarType...) {
    for i in vars {
        print(i);
    }
}


vari(vars: 1, 2, 4, 6, 8, 98)

// 修改函数参数
func swap(a: inout Int, b: inout Int) {
    let t = a;
    a = b;
    b = t;
}

var sA = 12;
var sB = 34;

swap(&sA, &sB);
sA
sB

// 函数指针，函数变量，函数当做另一个函数的参数
func sum(a: Int, b: Int) -> Int {
    return a + b
}
var addition: (Int, Int) -> Int = sum
print("输出结果: \(addition(40, 89))")

func mult(a: Int, b: Int) -> Int {
    return a*b;
}

func multiAdapterOperation(operation: (Int, Int)->Int, a: Int, b: Int)-> Int{
    return operation(a, b);
}

multiAdapterOperation(operation: sum, a: 12, b: 12);
multiAdapterOperation(operation: mult, a: 12, b: 12)


// 闭包
let studname = {print("clusure")};
studname();

let divide = {(val1: Int, val2: Int) -> Int in
    return val1/val2;
}
divide(3000, 12);

let names = ["AT", "AE", "D", "S", "BE"]
func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}
var reversed = names.sorted(by: backwards)
reversed = names.sorted(by: { (str1: String, str2: String) -> Bool in
    return str1<str2;
})
reversed;
// enumeration 枚举
enum DaysofaWeek {
    case Sunday
    case Monday
    case TUESDAY
    case WEDNESDAY
    case THURSDAY
    case FRIDAY
    case Saturday
}
var weekDay = DaysofaWeek.THURSDAY;
weekDay = .FRIDAY;//被设置过之后可以使用一个缩写语法设置为其他值
switch weekDay
{
case .Sunday:
    print("星期天")
case .Monday:
    print("星期一")
case .TUESDAY:
    print("星期二")
case .WEDNESDAY:
    print("星期三")
case .THURSDAY:
    print("星期四")
case .FRIDAY:
    print("星期五")
case .Saturday:
    print("星期六")
}

// 相关值
enum Student {
    case Name(String)
    case Mark(Int, Int, Int)
}
typealias Feet = Int

let aa: Feet = 100;
print(aa)

var optionalInteger: Int?
// Equal With:
var optionalInteger2: Optional<Int>

if a == 5 {
}
optionalInteger = 111;

print(optionalInteger!);///<可选值的强制解析
print(optionalInteger2);

optionalInteger!
//optionalInteger2!///Error 解析一个nil值

var optionalInteger3: Int!///<!定义了可以直接使用，自动解析
optionalInteger3 = 1213;
print(optionalInteger3);

let decimalInteger = 17;
let binaryInteger = 0b10001;
let octalInteger = 0o21;
let hexadecimalInteger = 0x11;

// 区间运算符
for index in 1...5 {
    print("\(index) ")
}

for index in 1..<5 {
    print("\(index) ")
}

var index = 10

switch index {
case 100  :
    print( "index 的值为 100")
case 10,15  :
    print( "index 的值为 10 或 15")
case 5  :
    print( "index 的值为 5")
default :
    print( "默认 case")
}


for index in 3 ..< 100 {
    
}

//Int[] arr = [1, 2];
//for i in arr {
//
//}

var i = 0;
while i<10 {
    i += 1;
    print("==i==\(i)");
}

var j = 0;
repeat {
    if j%5 == 0 {
        print("==j==\(j)");//初始值一个空的字符串。
    }
    j += 1;
} while(j<20);

// String
var strA: String = "你好";
var strB: String = "BB";

var strC = strA + strB;
print(strC + " lentgh = \(strC.characters.count)");

for code in strA.utf8 {
    print("\(code)")
}

for code in strA.utf16 {
    print("\(code)")
}

strA.isEmpty

strB.hasPrefix("A");
strB.hasPrefix("B");

Int("33");
Int("0x1111");


// 结构体
struct MarkStruct {
    var mark1: Int
    var mark2: Int
    
    init(mark1: Int, mark2: Int) {
        self.mark1 = mark1;
        self.mark2 = mark2;
    }
}

let markStruct = MarkStruct(mark1: 12, mark2:122);
markStruct.mark2;
markStruct.mark1;


struct studentMarks {
    var mark1 = 100
    var mark2 = 78
    var mark3 = 98
}
let marks = studentMarks()
print("Mark1 是 \(marks.mark1)")
print("Mark2 是 \(marks.mark2)")
print("Mark3 是 \(marks.mark3)")

class Student {
    var studentname: String;
    var mark: Int;
    
    init() {
        self.studentname = "";
        self.mark = 0;
    }
    
    init(stuname: String, mark: Int) {
        self.studentname = stuname;
        self.mark = mark;
    }
    
    
    func desc()->String{
        return "name: \(self.studentname)  mark:\(self.mark)"
    }
}


var stu = Student();
stu.mark;
stu.studentname;

var stu2 = Student(stuname: "aaa", mark: 123);
stu2.mark;
stu2.studentname;
stu2.desc();


//延迟存储属性一般用于：
//延迟对象的创建。
//当属性的值依赖于其他未知类
class sample {
    lazy var no = number() // `var` 关键字是必须的
}

var studentDetails = Student.Name("Nick");
var studentMarks = Student.Mark(123, 12, 989)
switch studentMarks {
case .Name(let name):
    print("name \(name)")
case .Mark(let mark1, let mark2, let mark3):
    print("MARKS \(mark1) \(mark2) \(mark3)")
    break
    
default: break
    
}


class Counter {
    var count:Int = 0;
    func increment()->Int {
        self.count += 1;
        return self.count;
    }
    
    func incrementBy(_ amount: Int) -> Int {
        self.count += amount;
        return self.count;
    }
    
    func reset() {
        self.count = 0;
    }
    
    func incrementBy(first no1: Int, no2: Int) -> Int {
        self.count += no1*no2;
        return self.count;
    }
}

let counter = Counter();
counter.increment();
counter.reset();
counter.incrementBy(10);
counter.increment();
counter.incrementBy(first: 12, no2: 12);

class Math {
    class func abs(number: Int) -> Int {
        if number<0 {
            return -number;
        }
        return number;
    }
    
    static func abs1(number: Int) -> Int {
        if number<0 {
            return -number;
        }
        return number;
    }
}

Math.abs(number: -120);
Math.abs1(number: 123);


// 对象下标
class DayofWeek {
    private var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "saturday"]
    
    subscript (index: Int) -> String {
        get {
            return days[index];
        }
        set (newValue) {
            self.days[index] = newValue;
        }
    }
}

var p = DayofWeek();
p[0];
p[0] = "OKOK"
p[0];

// 继承
class SuperClass {
    func show() {
        print("Super Class");
    }
}

class SubClass: SuperClass {
    override func show() {
        super.show();
        print("Sub Class");
    }
}

var sub = SubClass();
sub.show();

class number {
    var name = "Runoob Swift 教程"
}

var firstsample = sample()
print(firstsample.no.name)

class Sample2 {
    var no1 = 0.0, no2 = 0.0;
    var length = 300.0, breath = 150.0;
    
    var middle: (Double, Double) {
        get {
            return (self.length/2, self.breath/2);
        }
        set(axis) {
            no1 = axis.0 - (self.length/2);
            no2 = axis.1 - (self.breath/2);
        }
    }
}

var s = Sample2();
s.middle;

